package org.droid.campus.api.action;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.droid.campus.common.Action;
import org.droid.campus.common.ActionHandler;

public class GetCatDishAction extends Action implements ActionHandler {
	int catId = 0;
	public GetCatDishAction(ServletContext servletContext, String actionName) {
		super(servletContext, actionName);
		// TODO Auto-generated constructor stub
	}

	@Override
	public StringBuffer Do(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("请求参数forword："+request.getParameter("forword"));
		System.out.println("请求参数shopId："+request.getParameter("shopId"));
		System.out.println("请求参数catid："+request.getParameter("catId"));
		System.out.println("请求参数page："+request.getParameter("page"));
		catId = Integer.parseInt(request.getParameter("catId"));
		
			StringBuffer sb = new StringBuffer();
			try {
				URL url = null;
				// System.out.println(request.getServerName()+" "+request.getServerPort()+" "+request.getContextPath());
				if (456 == catId){
					url = new URL("http://"+request.getServerName()+":8080/"+request.getContextPath()+"/xml/dishes.xml");
				}else if (123 == catId){
					url = new URL("http://"+request.getServerName()+":8080/"+request.getContextPath()+"/xml/shopcat.xml");
				}else{
					return null;
				}
				HttpURLConnection hc = (HttpURLConnection) url.openConnection();
				InputStream is = hc.getInputStream();
			  	BufferedReader read = new BufferedReader(new InputStreamReader(is));
				String str = "";
				while ((str = read.readLine()) != null) {
					sb.append(str);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		
		return sb;
		
	}

	@Override
	public boolean prepare(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return true;
	}

}
